package com.fe.service.impl;

import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fe.mapper.PatriarchMapper;
import com.fe.mapper.TeacherMapper;
import com.fe.model.Patriarch;
import com.fe.model.Teacher;
import com.fe.service.LoginService;

@Service
public class LoginServiceImpl implements LoginService{
	
	@Autowired
	private TeacherMapper teacherMapper;
	@Autowired
	private PatriarchMapper patriarchMapper;
	
	/**
     * 
     * 方法描述：用户注册时，判断手机号是否已经被注册
     * @param user_tel
     * @return
     * @author ssh
	 * @throws Exception 
     * @date 2018年8月19日 下午12:40:44
     */
	@Override
	public void exictTel(String tel, String choose) {
		int result = -1;
		if (Objects.equals(choose, "teacher")) {
			result = teacherMapper.exictTel(tel);
		}else {
			result = patriarchMapper.exictTel(tel);
		}
		if (result > 0) {
			throw new RuntimeException("手机号已被注册!");
		}
	}

	/**
     * 
     * 方法描述：用户注册市，创建用户，并加入一点信息
     * @param user
     * @return
     * @author ssh
     * @date 2018年8月19日 下午1:14:54
     */
	@Override
	public void add(String choose, String tel, String password, Map<String, Object> map, HttpServletRequest request) {
		int result = -1;
		HttpSession session = request.getSession();
		if (Objects.equals(choose, "teacher")) {
			Teacher teacher = new Teacher();
			teacher.setTea_tel(tel);
			teacher.setTea_password(password);
			result = teacherMapper.add(teacher);
			//注册后，直接自动登陆，这样有id
			session.setAttribute("teacher", teacherMapper.loadByTelAndPassword(teacher));
		}else {
			Patriarch patriarch = new Patriarch();
			patriarch.setPat_tel(tel);
			patriarch.setPat_password(password);
			result = patriarchMapper.add(patriarch);
			//注册后，直接自动登陆，这样有id
			session.setAttribute("patriarch", patriarchMapper.loadByTelAndPassword(patriarch));
		}
		if (result < 0) {
			throw new RuntimeException("注册失败!");
		}
	}

	/**
     * 
     * 方法描述：登陆
     * @param choose 家教还是家长
     * @param password
     * @param map
     * @param request
     * @author ssh
     * @date 2018年8月19日 下午7:18:31
     */
	@Override
	public void login(String choose, String account, String password, Map<String, Object> map, HttpServletRequest request) {
		int result = -1;
		HttpSession session = request.getSession();
		if (Objects.equals(choose, "teacher")) {
			Teacher teacher = new Teacher();
			teacher.setTea_account(account);
			teacher.setTea_password(password);
			//登陆成功后，将登陆的账号加入到session中
			Teacher teacher2 = teacherMapper.loadByTelAndPassword(teacher);
			if (teacher2 != null) {
				session.setAttribute("teacher", teacher2);
				result = 1;
			}
			
		}else {
			Patriarch patriarch = new Patriarch();
			patriarch.setPat_account(account);
			patriarch.setPat_password(password);
			//登陆成功后，将登陆的账号加入到session中
			Patriarch patriarch2 = patriarchMapper.loadByTelAndPassword(patriarch);
			if (patriarch2 != null) {
				session.setAttribute("patriarch", patriarch2);
				result = 1;
			}
		}
		if (result < 0) {
			throw new RuntimeException("账号或密码错误!");
		}
	}
	
	 

}
